//
//  TixianVC.m
//  SSC
//
//  Created by 红沙尘 on 17/6/11.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "TixianVC.h"
#import "Tixian2VC.h"

@interface TixianVC ()
{
    __weak IBOutlet NSLayoutConstraint *lineA;
    __weak IBOutlet NSLayoutConstraint *lineB;
    __weak IBOutlet UIButton *tixianBtn;
    
    __weak IBOutlet UITextField *moneyField;
    __weak IBOutlet UITextField *psdField;
    __weak IBOutlet UILabel *descLabel;
    __weak IBOutlet UILabel *cardLabel;
    
    NSMutableArray *_cardArray;
}

@property (nonatomic, copy) NSDictionary *cardDict;

@end

@implementation TixianVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"用户提现"];
}

#pragma mark - 网络数据

- (void)cardList:(BOOL)showLoading {
    
    if (showLoading) [LoadingView show];
    
    [SSCRequest cardListSuccess:^(NSDictionary *responseObject) {
        if (showLoading) [LoadingView diss];
        
        NSArray *data = [NSString safeArray:responseObject[@"data"]];
        
        [_cardArray addObjectsFromArray:data];
        
        [self selectCardAtIndex:0];
        
    } fail:^(NSError *error) {
        
        [MTool showMessage:@"银行卡获取失败" inView:self.view];
        if (showLoading) [LoadingView diss];
    }];
}

#pragma mark - IBAction

- (IBAction)selectCard {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"银行卡"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    for (int i = 0; i < _cardArray.count; i ++) {
        NSDictionary *dict = _cardArray[i];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:dict[@"bankname"]
                                                         style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * _Nonnull action)
         {
             [self selectCardAtIndex:i];
        }];
        [alert addAction:action];
    }
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [self presentViewController:alert animated:YES completion:nil];
    
}

- (IBAction)gotoPsd {
    
    if ([moneyField.text floatValue] < 100) {
        [MTool showMessage:@"提现金额最少为100元" inView:self.view];
        return;
    }
    
    if (psdField.text.length == 0) {
        [MTool showMessage:@"请输入资金密码" inView:self.view];
        return;
    }
    
    if (!self.cardDict) {
        [MTool showMessage:@"请选择银行卡" inView:self.view];
        return;
    }
    
    [LoadingView show];
    
    [SSCRequest applyAmount:moneyField.text
                       type:@"0"
                   password:psdField.text
                     bankid:self.cardDict[@"id"]
                    success:^(NSDictionary *responseObject) {
        
                        [LoadingView diss];
                        [MTool showMessage:responseObject[@"msg"] inView:nil];
                        
                        [[Userinfo standardUserinfo] userMoney:^(NSString *money) {
                            descLabel.text = [NSString stringWithFormat:@"当前账户可提现金额 %@元",[Userinfo standardUserinfo].money];
                        }];
                        
    } fail:^(NSError *error) {
        [LoadingView diss];
    }];
}

- (IBAction)tixianAll {
    moneyField.text = [Userinfo standardUserinfo].money;
}

#pragma mark - event

- (void)selectCardAtIndex:(NSInteger)index {
    
    if (_cardArray.count == 0) return;
    
    self.cardDict = _cardArray[index];
    cardLabel.text = self.cardDict[@"bankname"];
}

#pragma mark - setup

- (void)setupUI {
    lineA.constant = LineHeight;
    lineB.constant = LineHeight;
    
    tixianBtn.layer.cornerRadius = 2;
    tixianBtn.layer.masksToBounds = YES;
    
    UILabel *leftLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
    leftLabel.text = @"输入资金密码:";
    leftLabel.font = [MTool fontOfSize:14];
    leftLabel.textColor = COLOR_999999;
    leftLabel.textAlignment = NSTextAlignmentRight;
    psdField.leftView = leftLabel;
    psdField.leftViewMode = UITextFieldViewModeAlways;
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 50)];
    label.text = @"￥";
    label.font = [MTool fontOfSize:18];
    moneyField.leftView = label;
    moneyField.leftViewMode = UITextFieldViewModeAlways;
    
    [[Userinfo standardUserinfo] userMoney:^(NSString *money) {
        descLabel.text = [NSString stringWithFormat:@"当前账户可提现金额 %@元",[Userinfo standardUserinfo].money];
    }];
}

- (void)setupData {
    
    _cardArray = [[NSMutableArray alloc] init];
    
    [self cardList:NO];
}

@end
